package com.dream21th.flow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dream21th.flow.biz.FlowableBiz;
import com.dream21th.flow.common.ComReq;
import com.dream21th.flow.common.ComResp;
import com.dream21th.flow.common.dto.FlowCommitDataDTO;
import com.dream21th.flow.common.dto.FlowStartDataDTO;
import com.dream21th.flow.common.dto.PageDTO;
import com.dream21th.flow.common.dto.QueryModelRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
@Api(tags = "流程相关操作")
@RestController
@RequestMapping("/v1")
public class FlowableController {

    @Autowired
    private FlowableBiz flowableBiz;

    @ApiOperation(value = "流程部署")
    @PostMapping("/deploy")
    public ComResp deploy(@RequestParam("file") MultipartFile file) {
        flowableBiz.deploy(file);
        return new ComResp().success();
    }

    @ApiOperation(value = "启动流程")
    @PostMapping("/startProcessInstance")
    public ComResp<String> startProcessInstance(@RequestBody ComReq<FlowStartDataDTO> dataDTO) {
        return new ComResp<String>().data(flowableBiz.startProcessInstance(dataDTO.getData())).success();
    }

    @ApiOperation(value = "任务提交")
    @PostMapping("/commitTask")
    public ComResp commitTask(@RequestBody ComReq<FlowCommitDataDTO> dataDTO) {
        flowableBiz.completeTask(dataDTO.getData());
        return new ComResp().success();
    }

    @ApiOperation(value = "待办任务查询")
    @PostMapping("/applyTasksPage")
    public ComResp<IPage<Map<String, Object>>> applyTasksPage(@RequestBody ComReq<PageDTO> dataDTO) {
        return new ComResp<IPage<Map<String, Object>>>().data(flowableBiz.applyTasksPage(dataDTO.getData())).success();
    }

    @ApiOperation(value = "已办任务查询")
    @PostMapping("/applyedTasksPage")
    public ComResp<IPage<Map<String, Object>>> applyedTasksPage(@RequestBody ComReq<PageDTO> dataDTO) {
        return new ComResp<IPage<Map<String, Object>>>().data(flowableBiz.applyedTasksPage(dataDTO.getData())).success();
    }

    @ApiOperation(value = "查询待领取任务列表")
    @PostMapping("/toAssignTasksPage")
    public ComResp<IPage<Map<String, Object>>> toAssignTasksPage(@RequestBody ComReq<PageDTO> dataDTO) {
        return new ComResp<IPage<Map<String, Object>>>().data(flowableBiz.toAssignTasksPage(dataDTO.getData())).success();
    }

    @ApiOperation(value = "领取任务")
    @PostMapping("/assignTask")
    public ComResp assignTask(@RequestBody ComReq<FlowCommitDataDTO> dataDTO) {
        flowableBiz.assignTask(dataDTO.getData());
        return new ComResp().success();
    }

    @ApiOperation(value = "获取流程图")
    @GetMapping("/flowImage/{processId}")
    public void flowImage(@PathVariable("processId") String processId, HttpServletResponse response) {
        try {
            byte[] b = flowableBiz.flowImage(processId);
            response.setHeader("Content-Type", "image/png");
            response.getOutputStream().write(b);
        } catch (Exception e) {
            log.error("ApiFormDetailReource-image:{}", e);
        }
    }

    @ApiOperation(value = "获取流程部署列表")
    @PostMapping("/getDeployModelPage")
    public ComResp<IPage<QueryModelRespDTO>> getDeployModelPage(@RequestBody ComReq<PageDTO> dataDTO) {
        return new ComResp<IPage<QueryModelRespDTO>>().data(flowableBiz.getDeployModelPage(dataDTO.getData())).success();
    }

    @ApiOperation(value = "获取流程定义流程图")
    @GetMapping("/flowDefImage/{processDefId}")
    @PreAuthorize("hasAuthority('USER')")
    public void flowDefImage(@PathVariable("processDefId") String processDefId, HttpServletResponse response) {
        try {
            byte[] b = flowableBiz.flowDefImage(processDefId);
            response.setHeader("Content-Type", "image/png");
            response.getOutputStream().write(b);
        } catch (Exception e) {
            log.error("ApiFormDetailReource-image:{}", e);
        }
    }
}
